package dao;

import java.util.ArrayList;

import modelo.Funcionario;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class DaoFuncionario {
	private SessionFactory fabrica;

	public DaoFuncionario()throws Exception
	{	fabrica=new Configuration().addClass(modelo.Funcionario.class).buildSessionFactory();
		
	}
	public void cadastrar(Funcionario f)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		sessao.save(f);
		transacao.commit();
		sessao.flush();
		sessao.close();
	}
	
	public ArrayList<Funcionario> consultarTodosNome(String nome){
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Funcionario where nome_func like '%" + nome + "%' order by nome_func");
		ArrayList<Funcionario> funcs=(ArrayList<Funcionario>)q.list();
		transacao.commit();
		sessao.flush();
		sessao.close();
		return funcs;
	}
	
	public ArrayList<Funcionario> consultarTodos(){
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Funcionario");
		ArrayList<Funcionario> funcs = (ArrayList<Funcionario>)q.list();
		transacao.commit();
		sessao.flush();
		sessao.close();
		return funcs;
	}
	
	
	public Funcionario consultar(Funcionario f)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		f=(Funcionario)sessao.get(modelo.Funcionario.class,f.getId_func());
		
		transacao.commit();
		sessao.flush();
		sessao.close();
		return f;
	}
	public void excluir(Funcionario f)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		sessao.delete(f);
		transacao.commit();
		sessao.flush();
		sessao.close();
	}
	public void alterar(Funcionario f)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		sessao.update(f);
		transacao.commit();
		sessao.flush();
		sessao.close();
	}
	public ArrayList consultarTodosSalarios(){
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Funcionario");
		ArrayList funcionarios=(ArrayList)q.list();
		transacao.commit();
		sessao.flush();
		sessao.close();
		return funcionarios;
	}
	public ArrayList consultarTodosTecnicos()
	{
		
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Funcionario where id_cargo = 4");
		ArrayList tecnicos=(ArrayList)q.list();
		transacao.commit();
		sessao.flush();
		sessao.close();
		
		return tecnicos;
	}
	

}
